/**
 * @Title: XunjianRecordController.java
 * @Package me.springboot.mybatis.web
 * @Description: TODO
 * @author zengh
 * @date 2018/04/23
 * @version V1.0
 */
package me.springboot.mybatis.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.springboot.mybatis.common.utils.JSONUtil;
import me.springboot.mybatis.common.utils.RoleType;
import me.springboot.mybatis.common.utils.SessionUtil;
import me.springboot.mybatis.common.utils.StringUtil;
import me.springboot.mybatis.core.RetCode;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.User;
import me.springboot.mybatis.model.XunjianRecord;
import me.springboot.mybatis.service.UserService;
import me.springboot.mybatis.service.XunjianRecordService;
import me.springboot.mybatis.web.req.XunJianReqRecord;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zengh
 * @ClassName: XunjianRecordController
 * @Description: TODO
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/xunjian/record")
@CrossOrigin
public class XunjianRecordController {

    @Resource
    private XunjianRecordService xunjianRecordService;

    @Resource
    private UserService userService;

    /**
     * @param xunjianRecord
     * @Title: add
     * @Description: 添加
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add")
    public RetResult<Object> add(XunjianRecord xunjianRecord) {
        xunjianRecordService.save(xunjianRecord);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param xunjianRecord
     * @Title: add2
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add2")
    public RetResult<Object> add2(@RequestBody XunjianRecord xunjianRecord) {
        xunjianRecordService.save(xunjianRecord);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param
     * @Title: delete
     * @Description: 删除
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) {
        xunjianRecordService.deleteById(id);
        return RetResponse.makeOKRsp();
    }

    /**
     * 上报故障
     *
     * @param request
     * @param xunjianRecord
     * @return
     * @throws ResultException
     */
    @PostMapping("/report")
    public RetResult<Object> report(HttpServletRequest request, XunjianRecord xunjianRecord) throws ResultException {
        xunjianRecord.setType(2);
        User user = SessionUtil.getSessionUser(request);
        xunjianRecord.setReportUserid(user.getuId());
        xunjianRecord.setXunjianUserid(user.getuId());
        xunjianRecord.setXunjianName(user.getName());
        xunjianRecord.setXunjianTime(new Date());
        xunjianRecord.setComplteTime(new Date());
        xunjianRecordService.save(xunjianRecord);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param xunjianRecord
     * @Title: update
     * @Description: 巡检故障上报
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update")
    public RetResult<Object> update(XunjianRecord xunjianRecord) {
        xunjianRecord.setStatus("1");
        xunjianRecord.setComplteTime(new Date());
        xunjianRecordService.update(xunjianRecord);
        return RetResponse.makeOKRsp();
    }

    /**
     * 巡检派单
     *
     * @param devicenums
     * @param userId
     * @return
     */
    @PostMapping("/dispatch")
    public RetResult<Object> dispatch(HttpServletRequest request,String xunjianTime, String devicenums, String userId) throws ResultException {
        if(!RoleType.isKehu(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        xunjianRecordService.dispatch(xunjianTime, devicenums, userId);
        return RetResponse.makeOKRsp();
    }

    /**
     * 转单
     *
     * @param xunjianRecordId
     * @param userId
     * @return
     * @throws ResultException
     */
    @PostMapping("/slip")
    public RetResult<Object> slip(HttpServletRequest request, String xunjianRecordId, String userId) throws ResultException {
        if(!RoleType.isKehu(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        xunjianRecordService.slip(xunjianRecordId, userId);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param xunjianRecord
     * @Title: update
     * @Description: 更新(传入参数为JSON格式)
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody XunjianRecord xunjianRecord) {
        xunjianRecordService.update(xunjianRecord);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param
     * @Title: detail
     * @Description: 根据ID查询详情
     * @Reutrn RetResult<XunjianRecord>
     */
    @GetMapping("/detail")
    public RetResult<Map<String, Object>> detail(@RequestParam Integer id) {
        XunjianRecord xunjianRecord = xunjianRecordService.findById(id);
        Map<String, Object> resultMap = JSONUtil.jsonToMap(JSONObject.toJSONString(xunjianRecord));
        if (StringUtil.isNotBlank(xunjianRecord.getXunjianUserid())) {
            Map<String, Object> query = new HashMap<>();
            query.put("uId", xunjianRecord.getXunjianUserid());
            List<User> users = userService.selectByMap(query);
            if (null != users && users.size() > 0) {
                resultMap.put("phone", users.get(0).getPhone());
            }
        }

        return RetResponse.makeOKRsp(resultMap);
    }

    /**
     * @param reqMap
     * @Title: detail
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<XunjianRecord>
     */
    @PostMapping("/detail2")
    public RetResult<XunjianRecord> detail(@RequestBody Map<String, Object> reqMap) {
        Integer id = Integer.valueOf(reqMap.get("id").toString());
        XunjianRecord xunjianRecord = xunjianRecordService.findById(id);
        return RetResponse.makeOKRsp(xunjianRecord);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: list
     * @Description: 获取每个人的巡检
     * @Reutrn RetResult<PageInfo<XunjianRecord>>
     */
    @GetMapping("/myXunjian")
    public RetResult<PageInfo<Map<String, Object>>> myXunjian(HttpServletRequest request, @RequestParam(defaultValue = "0") Integer page,
                                                              @RequestParam(defaultValue = "20") Integer size, XunJianReqRecord xunJianReqRecord) throws ResultException {
        PageHelper.startPage(page, size);
        User user = SessionUtil.getSessionUser(request);
        xunJianReqRecord.setXunjian_userid(user.getuId().toString());
        List<Map<String, Object>> list = xunjianRecordService.selectByReq(xunJianReqRecord);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    /**
     * 手机端获取要巡检的机器地图
     *
     * @param request
     * @param page
     * @param size
     * @return
     * @throws ResultException
     */
    @GetMapping("/xunjianAddress")
    public RetResult<PageInfo<Map<String, Object>>> xunjianAddress(HttpServletRequest request, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "20") Integer size) throws ResultException {
        PageHelper.startPage(page, size);
        User user = SessionUtil.getSessionUser(request);
        List<Map<String, Object>> list = xunjianRecordService.xunjianAddress(user.getuId());
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: list
     * @Description: 巡检list
     * @Reutrn RetResult<PageInfo<XunjianRecord>>
     */
    @PostMapping("/list")
    public RetResult<PageInfo<Map<String, Object>>> list(HttpServletRequest request, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "20") Integer size
            , XunJianReqRecord xunJianReqRecord) throws ResultException {
        PageHelper.startPage(page, size);
        if(RoleType.isKehu(request)) {
            xunJianReqRecord.setKehu_userid(SessionUtil.getSessionUser(request).getuId() + "");
        }
        List<Map<String, Object>> list = xunjianRecordService.selectByReq(xunJianReqRecord);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }
}